package com.dragon.web.commons.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.fintech.common.entity.ResultData;
import com.fintech.common.enums.ReturnCode;
import com.fintech.common.util.RedisUtils;
import com.fintech.common.vo.FintechResult;
import com.fintech.user.entity.User;

import net.sf.json.JSONObject;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		String accessToken = request.getHeader("accessToken");
		String idFintechUmUser = request.getHeader("idFintechUmUser");
		ResultData<?> resultData= RedisUtils.verifyUser(accessToken, idFintechUmUser,User.class);
		if(!resultData.getResultCode().equals(ReturnCode.success.getErrorCode())){
			JSONObject responseJSON = JSONObject.fromObject(FintechResult.getResult(resultData.getResultCode(), resultData.getMessage(), null));  
			response.setCharacterEncoding("UTF-8");  
			response.setContentType("application/json; charset=utf-8");  
			response.getWriter().print(responseJSON.toString());
			response.getWriter().flush();
		} else {
			return true;
		}
		return false;

			
	}
	
}
